package edu.uc.labs.MobilePrinting;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.xml.sax.SAXException;
import edu.uc.labs.MobilePrinting.Prefs.Preferences;
import edu.uc.labs.MobilePrinting.Prefs.PreferencesParser;

/**
 * 
 * @author Matt Silbernagel
 */
public class Configurator {

	private String os = System.getProperty("os.name");
	private String homeDir = System.getProperty("user.home");

	// //////////////////////////////////////////
	// CONSTANTS
	// //////////////////////////////////////////

	// SLEEP
	final int SLEEP = 10000;
	final int SLEEP_LONG = 30000;

	// FILES AND DIRS ( CONSTANTS )
	final public String LOGS = "logs";
	final public String PPD = "ppd";
	final public String CACHE = "cache";
	final public String ICONS = "icons";
	final public String DRIVERS = "drivers";
	final public String PREFS = "preferences";
	final public String XMLFILE = "printers.xml";
	final public String LOGFILE = "mobilePrinting.log";
	final public String TRAYICON = "MP_Tray_Win.gif";
	final public String TRAYICON_PROGRESS = "MP_Progress_0.gif";
	final public String TRAYICON_NIX = "MP_Tray_Nix.gif";
	final public String TRAYICON_MAC = "MP_Tray_Mac.gif";
	final public String DEFAULT_CONF = "defaults.xml";
	final public String USER_CONF = "user.xml";
	final public String DELIM = getDelim();
	final public String CACHEDIR = getCache();
	final public String OS = System.getProperty("os.name");
	
	
	// PRINTER, PRINTER SCRIPTS, ETC ( NOT CONSTANTS )
	public String PORTNAME = "UCitMobilePrinter";
	public String PRINTERNAME = null;
	public String WINSCRIPTSPATH = getPrinterScriptsPath();
	public String HTTP_SERVER = null;
	private String currentZone = null;

	// LOGGING
	private Level logLevel = Level.ERROR;
	
	// These variables are for the Preferences Object
	private String user_prefs = this.CACHEDIR + this.PREFS + this.DELIM + this.USER_CONF;
	private String default_prefs = this.CACHEDIR + this.PREFS + this.DELIM + this.DEFAULT_CONF;

	
	
	
	public Configurator() {
		
	}

	/**
	 * Method that sets up and returns a Preference Object created by either the
	 * User Prefs (if the file user.xml exists) or Default Prefs
	 * 
	 * @return Preference Object
	 */
	public Preferences getPrefs() {
		
		PreferencesParser prefParse;
		Preferences prefs = null;
		
		File prefs_file = new File(user_prefs);
		if (!prefs_file.exists()){
			// if a user created file doesn't exist, use the default
			prefs_file = new File(default_prefs);		
		}
		
		try {
			prefParse = new PreferencesParser();
			prefs = prefParse.parse(prefs_file);
			return prefs;
		} catch (ParserConfigurationException e) {
			LOGGER.error(e.toString());
		} catch (XPathExpressionException e) {
			LOGGER.error(e.toString());
		} catch (SAXException e) {
			LOGGER.error(e.toString());
		} catch (IOException e) {
			LOGGER.error(e.toString());
		}
		return prefs;
	}

	private String getDelim() {
		String delim = null;
		delim = (os.equalsIgnoreCase("Linux"))
				|| (os.equalsIgnoreCase("Mac OS X") || (os
						.equalsIgnoreCase("SunOS"))) ? "/" : "\\";
		return delim;
	}

	private String getCache() {
		String dir = null;
		if (os.equalsIgnoreCase("Linux") || os.equalsIgnoreCase("SunOS")) {
			dir = homeDir + "/.ucit/MobilePrinting/";
		} else if (os.equalsIgnoreCase("Mac OS X")) {
			dir = homeDir + "/Library/Application Support/UCit/MobilePrinting/";
		} else {
			dir = homeDir + "\\Application Data\\ucit\\MobilePrinting\\";
		}
		return dir;
	}

	private String getPrinterScriptsPath() {
		String dir = "";
		if (os.equalsIgnoreCase("Linux") || os.equalsIgnoreCase("Mac OS X")
				|| os.equalsIgnoreCase("SunOS")) {
			dir = "";
		} else if (os.equalsIgnoreCase("Windows XP")) {
			dir = "C:\\Windows\\system32";
		} else if (os.equalsIgnoreCase("Windows 7")) {
			dir = "C:\\WINDOWS\\system32\\Printing_Admin_Scripts\\en-US";
		}
		return dir;

	}

	public Logger setLogging() {
		Logger log = Logger.getLogger("MobilePrinting");
		try {

			PatternLayout layout = new PatternLayout("%d - %c - %p - %m%n");
			RollingFileAppender R = new RollingFileAppender(layout, CACHEDIR
					+ LOGS + DELIM + LOGFILE, false);
			R.setMaxFileSize("500KB");
			R.setMaxBackupIndex(1);

			ConsoleAppender c = new ConsoleAppender(layout);
			log.addAppender(R);
			log.addAppender(c);
			log.setLevel(logLevel);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return log;

	}

	public Logger getLogger() {
		return LOGGER;
	}

	public String getOS() {
		return os;
	}

	public String getHome() {
		return homeDir;
	}

	public void setZone(String currentZone) {
		this.currentZone = currentZone;
	}

	public String getZone() {
		return currentZone;
	}



}
